/*
 * SaleDAO.java
 *
 * Created on March 22, 2007, 3:59 PM
 */

package Data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * @author Dallin Regehr
 */
public class SaleDAO extends RevenueSourceDAO{
    
    /** Creates a new instance of SaleDAO */
    private static SaleDAO instance = null;
    
    /** Creates a new instance of RSSFeedDAO */
    private SaleDAO() {
    }
    
    public static synchronized SaleDAO getInstance() {
        if (instance == null) {
            instance = new SaleDAO();
        }
        return instance;
    }
    
    
    /////////////////////////////////
    ///   CREATE
    
    public SaleBO create() throws Exception {
        String id = GUID.generate();
        SaleBO sale = new SaleBO(id);
        Cache c = Cache.getInstance();
        c.put(sale.getId(), sale);
        return sale;
    }
    
    public SaleBO create(String id){
        SaleBO sale = new SaleBO(id);
        Cache c = Cache.getInstance();
        c.put(sale.getId(),sale);
        return sale;
    }
    
    
    /////////////////////////////////////
    ///   READ
    /**
     * This is the public read statement.  It loads an existing record
     * from the database.
     */
    public synchronized RevenueSourceBO read(String id) throws DataException {
        RevenueSourceBO sale;
        Connection conn;
        // check to see if id in the cache
        // if so, return it immediately
        if(Cache.getInstance().containsKey(id)){
            return (RevenueSourceBO) Cache.getInstance().get(id);
        }
        try {
            // retrieve a database connection from the pool
            conn = ConnectionPool.getInstance().get();
            // call read with a connection (the other read method in this class)
            sale = read(id,conn);
            // release the connection
            ConnectionPool.getInstance().release(conn);
            // rollback
            conn.rollback();
        }catch (ConnectionPoolException ex) {
            throw new DataException("Unable to communicate with the Database", ex);
            
        }catch (SQLException e) {
            throw new DataException("Could not retrieve record for id=" + id, e);
        }
        return sale;
    }
    /**
     *  This is a package method that is called by the public read (above) or
     *  by another DAO.  Either way we already have a connection to the database
     *  to use.  The user (controller) code never calls this one directly, since
     *  it can't know about Connection objects or SQLExceptions.
     */
    public RevenueSourceBO read(String id,Connection conn) throws DataException {
        SaleBO sale = null;
        // check the cache to see if the customer is in the cache
        // if so, return it immediately
        if(Cache.getInstance().containsKey(id)){
            return (RevenueSourceBO) Cache.getInstance().get(id);
        }
        
        try{
            sale = create(id);
            Cache.getInstance().put(sale.getId(),sale);
            super.read(id,conn);
            
            
            
            PreparedStatement pstmt =conn.prepareStatement("Select * from \"sale\" WHERE id=?");
            pstmt.setString(1,id);
            
            ResultSet rs = pstmt.executeQuery();
            // get a rs from an sql SELECT statement
            // if( rs.next()){convert the rs to the sale}
            if(rs.next()){
                sale.setQuantity(rs.getInt("Quantity"));
                
                //save to the cache
                sale.setAlreadyInDB(true);
                Cache.getInstance().put(sale.getId(),sale);
                
                // else {handling for no customer with that GUID
            }
        }catch(SQLException e){
            //rollback
            throw new DataException("Could not retrieve record for id=" + id,e);
        }
        //release the connection back to the pool.
        return (RevenueSourceBO) sale;
    }
    
    //////////////////////////////////
    ///   UPDATE
    
    /**
     * This is the public save method.  It is what is called when
     * the user (controller) code wants to save or update an object
     * into the database.
     */
    public synchronized void save(SaleBO sale) throws DataException {
        
        try {
            // retrieve a database connection from the pool
            Connection conn = ConnectionPool.getInstance().get();
            // call save with a connection (the other save method in this class)
            save(sale, conn);
            // release the connection
            ConnectionPool.getInstance().release(conn);
        }catch (SQLException e) {
            // rollback
            throw new DataException("Could not save record for id=" + sale.getId(), e);
        }catch (ConnectionPoolException e){
            throw new DataException("Could not retrieve Connection", e);
        }
        
        // release the connection back to the pool
        
    }
    
    
    /**
     *  This is a package method that is called by the public save (above) or
     *  by another DAO.  Either way we already have a connection to the database
     *  to use.  The user (controller) code never calls this one directly, since
     *  it can't know about Connection objects or SQLExceptions.
     *
     *  By having other DAOs call this save method (rather than update or
     *  insert below, each DAO in a chained save (like the CustomerDAO calling
     *  MembershipDAO calling InterestDAO save chain) can independently decide
     *  whether to udpate or insert the BO it's saving.  That's why I made
     *  update and insert private rather than package level -- if you call
     *  them directly from another DAO, this DAO can't decide whether it's
     *  object needs to be inserted or updated.
     */
    synchronized void save(SaleBO sale, Connection conn) throws SQLException, DataException {
        // check the dirty flag in the object.  if it is dirty,
        // run update or insert
        if (sale.isDirty()) {
            if (sale.isAlreadyInDB()) {
                update(sale, conn);
            }else{
                insert(sale, conn);
            }
            // set the dirty flag to false now that we've saved it
            sale.setDirty(false);
        }
        
        // call save(bo, conn) on any subobjects (like CustomerDAO to saleshipDAO)
        /*This next line was removed because we don't have all of the code to remediate the circulare saves.
         CustomerDAO.getInstance().save(sale.getCustomer(),conn);
         **/
        // touch the cache for the object
    }
    
    /**
     * This method is really a part of save(bo, conn) above.  It could be
     * embedded directly in it, but I've separated it into it's own method
     * to isolate the SQL udpate statement and make it more readable.  But
     * logically, it's really just a part of save.
     */
    private synchronized void update(SaleBO sale,Connection conn) throws SQLException, DataException {
        super.save(sale);
        // do the update statement
        PreparedStatement pstmt =conn.prepareStatement("UPDATE \"Sale\" SET \"Quantity\" = ?," +
                " WHERE \"ID\" = ?");
        pstmt.setInt(1,sale.getQuantity());
        pstmt.setString(2,sale.getId());
        
        pstmt.executeUpdate();
        
        //Do the saleship interests insertion
        
        System.out.println("saleUpdateSuccessful");
    }
    /**
     * This method is really a part of save(bo, conn) above.  It could be
     * embedded directly in it, but I've separated it into it's own method
     * to isolate the SQL insert statement and make it more readable.  But
     * logically, it's really just a part of save.
     */
    private synchronized void insert(SaleBO sale,Connection conn) throws SQLException, DataException {
        super.save(sale);
        // do the insert SQL statement
        PreparedStatement pstmt =conn.prepareStatement("INSERT INTO \"Sale\" VALUES (?,?)");
        pstmt.setString(1,sale.getId());
        pstmt.setInt(2,sale.getQuantity());
        
        pstmt.executeUpdate();
        System.out.println("saleInsertSucessful");
        
        //Do the saleship interests insertion
        
        // tell the object that it's now in the db (so we call update next time not insert)
        sale.setAlreadyInDB(true);
    }
    
    ////////////////////////////////////
    ///   DELETE
    
    // We have decided not to support delete because we would like the sales to stay in the database for data archiving.
    
    //////////////////////////////
    ///  SEARCH methods
    
    public List<String[]> getAll() throws SQLException,DataException {
        // get the connection
        Connection conn=null;
        try {
            conn=ConnectionPool.getInstance().get();
        } catch (ConnectionPoolException e){
            throw new DataException("Could not retrieve Connection", e);
        }
        List<String[]> saleList = new ArrayList<String[]>();
        
        // sql the names, phone, and ids
        PreparedStatement pstmt =conn.prepareStatement("Select * from \"Sale\"");
        ResultSet rs = null;
        rs = pstmt.executeQuery();
        // get a rs from an sql SELECT statement
        if(rs.next()){
            do{
                String[] temp= new String[2];
                temp[0]= rs.getString("ID");
                temp[1]= rs.getString("Quantity");
                saleList.add(temp);
                
            }while(rs.next());
            
        }else{
            throw new DataException("List was unable to be retrieved.");
        }
        
        
        // while loop to populate the list from the results
        
        // release my connection
        
        // return the list
        return saleList;
    }
    
}